/*
 * #%L
 * SwiftUICommonUtilities
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
package com.linkwithweb.products.swiftui.transactions;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;

/**
 * @author ashwin kumar
 *         <p>
 *         This source comes from <a href =
 *         "http://jira.springframework.org/browse/SPR-6237">here</a>.
 *         </p>
 */
public class MultiTransactionStatus implements TransactionStatus {

	private PlatformTransactionManager mainTransactionManager;

	private Map<PlatformTransactionManager, TransactionStatus> transactionStatuses = Collections
			.synchronizedMap(new HashMap<PlatformTransactionManager, TransactionStatus>());

	private boolean newSynchonization;

	public Map<PlatformTransactionManager, TransactionStatus> getTransactionStatuses() {
		return transactionStatuses;
	}

	public MultiTransactionStatus(PlatformTransactionManager mainTransactionManager) {
		this.mainTransactionManager = mainTransactionManager;
	}

	private TransactionStatus getHlavniTM() {
		return transactionStatuses.get(mainTransactionManager);
	}

	public void setNewSynchonization() {
		this.newSynchonization = true;
	}

	public boolean isNewSynchonization() {
		return newSynchonization;
	}

	// @Override
	public boolean isNewTransaction() {
		return getHlavniTM().isNewTransaction();
	}

	// @Override
	public boolean hasSavepoint() {
		return getHlavniTM().hasSavepoint();
	}

	// @Override
	public void setRollbackOnly() {
		for (TransactionStatus ts : transactionStatuses.values()) {
			ts.setRollbackOnly();
		}
	}

	// @Override
	public boolean isRollbackOnly() {
		return getHlavniTM().isRollbackOnly();
	}

	// @Override
	public boolean isCompleted() {
		return getHlavniTM().isCompleted();
	}

	// @Override
	public Object createSavepoint() throws TransactionException {
		throw new RuntimeException("Savepoint not supported");
	}

	// @Override
	public void rollbackToSavepoint(Object savepoint) throws TransactionException {
		for (TransactionStatus ts : transactionStatuses.values()) {
			ts.rollbackToSavepoint(savepoint);
		}
	}

	// @Override
	public void releaseSavepoint(Object savepoint) throws TransactionException {
		for (TransactionStatus ts : transactionStatuses.values()) {
			ts.releaseSavepoint(savepoint);
		}
	}

	// @Override
	public void flush() {
	}
}